package org.example.develop;

import org.apache.spark.sql.api.java.UDF2;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName：HealthAssessmentModelUDF
 * @Descripton：
 * @Author：Mr.LL
 * @Creation Date：2023-04-07 16:33
 * @Version: 1.0V
 */
public class HealthAssessmentModelUDF implements UDF2<String,String,String> {
    @Override
    public String call(String mxmc, String phm_gzmc) throws Exception {
        String[] gzmcArrays = phm_gzmc.split(",");
        int gzmcLength = gzmcArrays.length;
        StringBuffer sbResult = new StringBuffer();

        //A,B,C判断结果字段
        String estimateResult = "";
        //轴判断字段
        String zhouResult = "";

        //10011
        if (mxmc.equals("10011")) {
            if(gzmcLength > 0){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10012
        if (mxmc.equals("10012")) {
            if(gzmcLength > 0){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10013
        if (mxmc.equals("10013")) {
            if(gzmcLength > 0){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10014
        if (mxmc.equals("10014")) {
            if(gzmcLength > 0){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10015
        if (mxmc.equals("10015")) {
            if(gzmcLength > 0){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10016
        if (mxmc.equals("10016")) {
            Set<String> list = new HashSet<>();
            for (int i = 0; i < gzmcLength; i++) {
                if(gzmcArrays[i].contains("变流器1L1输入过流")){
                    list.add(gzmcArrays[i] + "(1轴)");
                }
                if(gzmcArrays[i].equals("变流器1L2输入过流")){
                    list.add(gzmcArrays[i] + "(2轴)");
                }
                if(gzmcArrays[i].equals("变流器1L3输入过流")){
                    list.add(gzmcArrays[i] + "(3轴)");
                }
                if(gzmcArrays[i].equals("变流器2L1输入过流")){
                    list.add(gzmcArrays[i] + "(6轴)");
                }
                if(gzmcArrays[i].equals("变流器2L2输入过流")){
                    list.add(gzmcArrays[i] + "(5轴)");
                }
                if(gzmcArrays[i].equals("变流器2L3输入过流")){
                    list.add(gzmcArrays[i] + "(4轴)");
                }
            }
            StringBuilder sb = new StringBuilder();
            //判断获取轴信息,1定义正则,2循环拼接并截取数字,3去掉末尾的逗号
            Pattern pattern = Pattern.compile("\\d+轴");
            Matcher matcher = pattern.matcher(list.toString());
            while (matcher.find()) {sb.append(matcher.group().substring(0,1)).append(",");}
            //轴排序
            String[] arr = sb.toString().replaceAll(",$", "").split(",");
            Arrays.sort(arr);
            zhouResult = String.join(",",arr);
            //判断获取评价结果
            if(gzmcLength > 0 && gzmcLength < 3){
                estimateResult = "B";
            }else if(gzmcLength == 3){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10017
        if (mxmc.equals("10017")) {
            StringBuilder sb = new StringBuilder();
            //判断获取轴信息,1定义正则,2循环拼接并截取数字,3去掉末尾的逗号
            Pattern pattern = Pattern.compile("\\d+轴");
            Matcher matcher = pattern.matcher(phm_gzmc);
            while (matcher.find()) {sb.append(matcher.group().substring(0,1)).append(",");}
            //轴排序
            String[] arr = sb.toString().replaceAll(",$", "").split(",");
            Arrays.sort(arr);
            zhouResult = String.join(",",arr);
            //判断获取评价结果
            if(gzmcLength > 0){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10018
        if (mxmc.equals("10018")) {
            StringBuilder sb = new StringBuilder();
            //判断获取轴信息,1定义正则,2循环拼接并截取数字,3去掉末尾的逗号
            Pattern pattern = Pattern.compile("\\d+轴");
            Matcher matcher = pattern.matcher(phm_gzmc);
            while (matcher.find()) {sb.append(matcher.group().substring(0,1)).append(",");}
            //轴排序
            String[] arr = sb.toString().replaceAll(",$", "").split(",");
            Arrays.sort(arr);
            zhouResult = String.join(",",arr);
            //判断获取评价结果
            if(gzmcLength > 0){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10019
        if (mxmc.equals("10019")) {
            StringBuilder sb = new StringBuilder();
            //判断获取轴信息,1定义正则,2循环拼接并截取数字,3去掉末尾的逗号
            Pattern pattern = Pattern.compile("\\d+轴");
            Matcher matcher = pattern.matcher(phm_gzmc);
            while (matcher.find()) {sb.append(matcher.group().substring(0,1)).append(",");}
            //轴排序
            String[] arr = sb.toString().replaceAll(",$", "").split(",");
            Arrays.sort(arr);
            zhouResult = String.join(",",arr);
            //判断获取评价结果
            if(gzmcLength > 0 && gzmcLength < 3){
                estimateResult = "B";
            }else if(gzmcLength == 3){
                estimateResult = "C";
            }else{
                estimateResult = "A";
            }
        }
        //10020
        if (mxmc.equals("10020")) {
            if(gzmcLength > 0){
                estimateResult = "B";
            }else{
                estimateResult = "A";
            }
        }

        sbResult.append(estimateResult + ";").append(zhouResult);
        return sbResult.toString();
    }
}
